// The contents of this file are subject to the Mozilla Public License Version
// 1.1
//(the "License"); you may not use this file except in compliance with the
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
//Software distributed under the License is distributed on an "AS IS" basis,
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//for the specific language governing rights and
//limitations under the License.
//
//The Original Code is "The Columba Project"
//
//The Initial Developers of the Original Code are Frederik Dietz and Timo
// Stich.
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
//
//All Rights Reserved.
package org.columba.mail.gui.frame;

import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreePath;

import org.columba.addressbook.gui.dialog.contact.ResourceDialog;
import org.columba.api.gui.frame.IContainer;
import org.columba.api.gui.frame.IDock;
import org.columba.api.gui.frame.IDockable;
import org.columba.api.selection.ISelectionListener;
import org.columba.api.selection.SelectionChangedEvent;
import org.columba.core.config.ViewItem;
import org.columba.core.gui.base.UIFSplitPane;
import org.columba.core.gui.menu.MenuXMLDecoder;
import org.columba.core.gui.tagging.TagList;
import org.columba.core.gui.tagging.TagPopupMenu;
import org.columba.core.io.DiskIO;
import org.columba.mail.command.IMailFolderCommandReference;
import org.columba.mail.config.MailConfig;
import org.columba.mail.folder.IMailFolder;
import org.columba.mail.folder.IMailbox;
import org.columba.mail.folder.IMailboxInfo;
import org.columba.mail.folder.event.IFolderEvent;
import org.columba.mail.folder.event.IFolderListener;
import org.columba.mail.gui.composer.HeaderController;
import org.columba.mail.gui.filtertoolbar.FilterToolbar;
import org.columba.mail.gui.message.action.ViewMessageAction;
import org.columba.mail.gui.socialnet.ISocialNetController;
import org.columba.mail.gui.socialnet.SocialNetController;
import org.columba.mail.gui.socialnet.selection.VertexSelectionChangedEvent;
import org.columba.mail.gui.socialnet.selection.VertexSelectionHandler;
import org.columba.mail.gui.table.ITableController;
import org.columba.mail.gui.table.TableController;
import org.columba.mail.gui.table.action.DeleteAction;
import org.columba.mail.gui.table.action.OpenMessageWithComposerAction;
import org.columba.mail.gui.table.action.OpenMessageWithMessageFrameAction;
import org.columba.mail.gui.table.action.ViewHeaderListAction;
import org.columba.mail.gui.table.model.HeaderTableModel;
import org.columba.mail.gui.table.model.MessageNode;
import org.columba.mail.gui.table.selection.ResourceTableSelectionChangedEvent;
import org.columba.mail.gui.table.selection.ResourceTableSelectionHandler;
import org.columba.mail.gui.table.selection.TableSelectionChangedEvent;
import org.columba.mail.gui.table.selection.TableSelectionHandler;
import org.columba.mail.gui.tagging.MailTagList;
import org.columba.mail.gui.tree.FolderTreeModel;
import org.columba.mail.gui.tree.ITreeController;
import org.columba.mail.gui.tree.TreeController;
import org.columba.mail.gui.tree.action.MoveDownAction;
import org.columba.mail.gui.tree.action.MoveUpAction;
import org.columba.mail.gui.tree.action.RenameFolderAction;
import org.columba.mail.gui.tree.action.SortFoldersMenu;
import org.columba.mail.gui.tree.selection.TreeSelectionChangedEvent;
import org.columba.mail.gui.tree.selection.TreeSelectionHandler;
import org.columba.mail.util.MailResourceLoader;
import org.python.modules.newmodule;

import com.deruan.hotcore.resource.action.SeeSocialNodeAction;
import com.deruan.hotcore.resource.action.ViewResourceAction;
import com.deruan.hotcore.resource.command.ResultTreeSelectionChangedEvent;
import com.deruan.hotcore.resource.gui.tree.IResultTreeController;
import com.deruan.hotcore.resource.gui.tree.ResourceTreeNode;
import com.deruan.hotcore.resource.gui.tree.ResultRootNode;
import com.deruan.hotcore.resource.gui.tree.ResultSelectionHandler;
import com.deruan.hotcore.resource.gui.tree.ResultTreeNode;
import com.deruan.hotcore.resource.gui.tree.SearchResultTreeController;
import com.deruan.hotcore.resource.table.IResourceTableController;
import com.deruan.hotcore.resource.table.ResourceTableController;
import com.deruan.hotcore.resource.table.model.ResourceHeaderTableModel;

import edu.uci.ics.jung.visualization.GraphZoomScrollPane;

/**
 * @author fdietz
 */
public class ThreePaneMailFrameController extends AbstractMailFrameController
		implements TreeViewOwner, TableViewOwner,IResourceTableOwner,SocialNetViewOwner, ISelectionListener,
		IFolderListener {
	
	public SocialNetController snController;

	public TreeController treeController;
	
	public SearchResultTreeController resourceSearchTreeController;

	public TableController tableController;
	
	public ResourceTableController resourceTableController;

	public HeaderController headerController;

	public FilterToolbar filterToolbar;

	public JSplitPane mainSplitPane;

	public JSplitPane rightSplitPane;

	private JPanel tablePanel;

	private JPanel messagePanel;

	private IMailFolder currentFolder;

	/**
	 * true, if the messagelist table selection event was triggered by a popup
	 * event. False, otherwise.
	 */
	public boolean isTablePopupEvent;

	/**
	 * true, if the tree selection event was triggered by a popup event. False,
	 * otherwise.
	 */
	public boolean isTreePopupEvent;

	private IDockable folderTreeDockable;
	
	private IDockable resultTreeDockable;

	private IDockable messageListDockable;

	private IDockable messageViewerDockable;

	private IDockable tagListDockable;
	
	private IDockable socialNetViewDockable;
	
	private IDockable resourceListDockable;

	/**
	 * @param viewItem
	 */
	public ThreePaneMailFrameController(ViewItem viewItem) 
	{
		super(viewItem);
		snController = new SocialNetController(this);
		treeController = new TreeController(this, FolderTreeModel.getInstance());
		tableController = new TableController(this);
		resourceTableController = new ResourceTableController(this);
        resourceSearchTreeController = new SearchResultTreeController(this); 
		
		// create selection handlers
		TableSelectionHandler tableHandler = new TableSelectionHandler(tableController);
		getSelectionManager().addSelectionHandler(tableHandler);
		tableHandler.addSelectionListener(this);
		
		ResourceTableSelectionHandler rsTableHandler = new ResourceTableSelectionHandler(resourceTableController);
		getSelectionManager().addSelectionHandler(rsTableHandler);
		rsTableHandler.addSelectionListener(this);

		TreeSelectionHandler treeHandler = new TreeSelectionHandler(treeController.getView());
		getSelectionManager().addSelectionHandler(treeHandler);
		
		//For result search 
		ResultSelectionHandler resultHandler = new ResultSelectionHandler(resourceSearchTreeController.getView());
		getSelectionManager().addSelectionHandler(resultHandler);
		resultHandler.addSelectionListener(this);
		
		
		VertexSelectionHandler vertexHandler = new VertexSelectionHandler(snController.getView());
		getSelectionManager().addSelectionHandler(vertexHandler);
		vertexHandler.addSelectionListener(this);
		// double-click mouse listener
		tableController.getView().addMouseListener(new TableMouseListener());

		resourceTableController.getView().addMouseListener(new ResourceTableMouseListener());
		
		treeController.getView().addMouseListener(new TreeMouseListener());

		snController.getView().addMouseListener(new SocialNetMouseListener());
		
		resourceSearchTreeController.getView().addMouseListener(new SearchResultMouseListener());
		
		// table registers interest in tree selection events
		treeHandler.addSelectionListener(tableHandler);
		vertexHandler.addSelectionListener(tableHandler);
		
		//
		vertexHandler.addSelectionListener(rsTableHandler);

		// also register interest in tree seleciton events
		// for updating the title
		treeHandler.addSelectionListener(this);

		filterToolbar = new FilterToolbar(this);

		RenameFolderAction renameFolderAction = new RenameFolderAction(this);

		// Register F2 hotkey for renaming folder when the message panel has
		// focus
		tableController.getView().getActionMap().put("F2", renameFolderAction);
		tableController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "F2");

		// Register F2 hotkey for renaming folder when the folder tree itself
		// has focus
		treeController.getView().getActionMap().put("F2", renameFolderAction);
		treeController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "F2");

		// Register Alt-Up hotkey for moving up folder when folder tree or
		// table have focus
		MoveUpAction moveUpAction = new MoveUpAction(this);
		tableController.getView().getActionMap().put("ALT_UP", moveUpAction);
		tableController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK),
				"ALT_UP");

		treeController.getView().getActionMap().put("ALT_UP", moveUpAction);
		treeController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_UP, KeyEvent.ALT_MASK),
				"ALT_UP");

		// Register Alt-Down hotkey for moving up folder when folder tree or
		// table have focus
		MoveDownAction moveDownAction = new MoveDownAction(this);
		tableController.getView().getActionMap()
				.put("ALT_DOWN", moveDownAction);
		tableController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK),
				"ALT_DOWN");

		treeController.getView().getActionMap().put("ALT_DOWN", moveDownAction);
		treeController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, KeyEvent.ALT_MASK),
				"ALT_DOWN");

		DeleteAction deleteAction = new DeleteAction(this);
		tableController.getView().getActionMap().put("DEL", deleteAction);
		tableController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "DEL");


		OpenMessageWithMessageFrameAction openAction = new OpenMessageWithMessageFrameAction(this);
		tableController.getView().getActionMap().put("ENTER", openAction);
		tableController.getView().getInputMap().put(
				KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ENTER");

		registerDockables();

		tableController.createPopupMenu();
		treeController.createPopupMenu();
		snController.createPopupMenu();
		resourceSearchTreeController.createPopupMenu();
		resourceTableController.createResourcePopupMenu();
		// messageController.createPopupMenu();

	}

	public void enableMessagePreview(boolean enable) 
	{
		getViewItem().setBoolean("header_enabled", enable);
		if (enable) {
			rightSplitPane = new UIFSplitPane();
			rightSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			rightSplitPane.add(tablePanel, JSplitPane.LEFT);
			rightSplitPane.add(messagePanel, JSplitPane.RIGHT);
			mainSplitPane.add(rightSplitPane, JSplitPane.RIGHT);
		} else {
			rightSplitPane = null;

			mainSplitPane.add(tablePanel, JSplitPane.RIGHT);
		}

		mainSplitPane.setDividerLocation(viewItem.getIntegerWithDefault(
				"splitpanes", "main", 100));

		if (enable)
			rightSplitPane.setDividerLocation(viewItem.getIntegerWithDefault(
					"splitpanes", "header", 100));

		fireLayoutChanged();
	}

	/**
	 * @return Returns the filterToolbar.
	 */
	public FilterToolbar getFilterToolbar() {
		return filterToolbar;
	}

	/**
	 * @see org.columba.mail.gui.frame.TreeViewOwner#getTreeController()
	 */
	public ITreeController getTreeController() {
		return treeController;
	}
	
	public IResultTreeController getResultTreeController()
	{
		return resourceSearchTreeController;
	}
	
	public ISocialNetController getSocialNetController() {
		return snController;
	}

	/**
	 * @see org.columba.mail.gui.frame.TableViewOwner#getTableController()
	 */
	public ITableController getTableController() {
		return tableController;
	}
	
	public IResourceTableController getResourceTableController(){
		return resourceTableController;
	}

	public void showFilterToolbar() 
	{
		tablePanel.add(filterToolbar, BorderLayout.NORTH);
		tablePanel.validate();
	}

	public void hideFilterToolbar() {
		tablePanel.remove(filterToolbar);
		tablePanel.validate();

	}

	/**
	 * @see org.columba.api.gui.frame.IFrameMediator#getString(java.lang.String,
	 *      java.lang.String, java.lang.String)
	 */
	public String getString(String sPath, String sName, String sID) 
	{
		return MailResourceLoader.getString(sPath, sName, sID);
	}

	/**
	 * @see org.columba.api.selection.ISelectionListener#selectionChanged(org.columba.api.selection.SelectionChangedEvent)
	 */
	public void selectionChanged(SelectionChangedEvent e) {

		if (e instanceof TreeSelectionChangedEvent) {
			// tree selection event
			TreeSelectionChangedEvent event = (TreeSelectionChangedEvent) e;

			IMailFolder[] selectedFolders = event.getSelected();

			if (isTreePopupEvent == false) {
				// view headerlist in message list viewer
				new ViewHeaderListAction(this).actionPerformed(null);

				// Unregister/register as Folder listener
				if (currentFolder != null) {
					currentFolder.removeFolderListener(this);
					currentFolder = null;
				}
				if (selectedFolders.length == 1 && selectedFolders[0] != null) {
					selectedFolders[0].addFolderListener(this);
					currentFolder = selectedFolders[0];
				}

				// update frame title
				updateTreeDockableTitle();
			}

			isTreePopupEvent = false;

		}else if(e instanceof ResultTreeSelectionChangedEvent)
		{
			new SeeSocialNodeAction(this).actionPerformed(null);
            //show resource content
			//new ViewResourceAction(this).actionPerformed(new ActionEvent(e, 1, ""));
			//isTreePopupEvent = false;
		}
		else if (e instanceof TableSelectionChangedEvent) {
			if (isTablePopupEvent == false)
				// show message content
				new ViewMessageAction(this).actionPerformed(null);

			isTablePopupEvent = false;
		} else if(e instanceof VertexSelectionChangedEvent)
		{
			VertexSelectionChangedEvent event = (VertexSelectionChangedEvent) e;
			IMailFolder selectedFolder = event.getSelectedFolder();	
			new ViewHeaderListAction(this).actionPerformed(new ActionEvent(event, 1, ""));
			// Unregister/register as Folder listener
			if (currentFolder != null) 
			{
				currentFolder.removeFolderListener(this);
				currentFolder = null;
			}
			if (selectedFolder != null) {
				selectedFolder.addFolderListener(this);
				currentFolder = selectedFolder;
			}
		}
		else if(e instanceof ResourceTableSelectionChangedEvent)
		{
            //show resource content
			//new ViewResourceAction(this).actionPerformed(null);
			//isTablePopupEvent = false;
		}
		else
		{
			throw new IllegalArgumentException(
			"unknown selection changed event");
		}		
	}

	private void updateTreeDockableTitle() {
		if (currentFolder != null) {
			fireTitleChanged(currentFolder.getName());

			// update message list view title
			messageListDockable.setTitle(currentFolder.getName());

			// simply demonstration of how to change the docking title
			if (currentFolder instanceof IMailbox) {
				IMailboxInfo info = ((IMailbox) currentFolder)
						.getMessageFolderInfo();
				StringBuffer buf = new StringBuffer();
				buf.append("Total: " + info.getExists());
				buf.append(" Recent: " + info.getRecent());
				folderTreeDockable.setTitle(buf.toString());
			} else
				folderTreeDockable.setTitle(currentFolder.getName());
		} else {
			fireTitleChanged("");
		}
	}

	/**
	 * Double-click mouse listener for message list table component. <p/> If
	 * message is marked as draft, the composer will be opened to edit the
	 * message. Otherwise, the message will be viewed in the message frame.
	 *
	 * @author Frederik Dietz
	 */
	class TableMouseListener extends MouseAdapter {

		/**
		 * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
		 */
		public void mousePressed(MouseEvent event) 
		{
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		public void mouseReleased(MouseEvent event) 
		{
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
		 */
		public void mouseClicked(MouseEvent event) {
			// if mouse button was pressed twice times
			if (event.getClickCount() == 2) 
			{
				// get selected row
				int selectedRow = tableController.getView().getSelectedRow();
				// get message node at selected row
				MessageNode node = (MessageNode)((HeaderTableModel) tableController
						.getHeaderTableModel()).getMessageNodeAtRow(selectedRow);

				// is the message marked as draft ?
				boolean markedAsDraft = node.getHeader().getFlags().getDraft();

				if (markedAsDraft) {
					// edit message in composer
					new OpenMessageWithComposerAction(
							ThreePaneMailFrameController.this)
							.actionPerformed(null);
				} else {
					// open message in new message-frame
					new OpenMessageWithMessageFrameAction(
							ThreePaneMailFrameController.this)
							.actionPerformed(null);
				}
			}
		}

		protected void processPopup(final MouseEvent event) {

			isTablePopupEvent = true;

			JTable table = tableController.getView();

			int selectedRows = table.getSelectedRowCount();

			if (selectedRows <= 1&&selectedRows>0) {
				// select node
				int row = table.rowAtPoint(new Point(event.getX(), event.getY()));
				table.setRowSelectionInterval(row, row);
			}

			SwingUtilities.invokeLater(new Runnable() {

				public void run() {
					tableController.getPopupMenu().show(event.getComponent(),
							event.getX(), event.getY());
					isTablePopupEvent = false;
				}
			});
		}
	}
	
	
	/**
	 * Double-click mouse listener for message list table component. <p/> If
	 * message is marked as draft, the composer will be opened to edit the
	 * message. Otherwise, the message will be viewed in the message frame.
	 *
	 * @author Frederik Dietz
	 */
	class ResourceTableMouseListener extends MouseAdapter {

		/**
		 * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
		 */
		public void mousePressed(MouseEvent event) 
		{
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		public void mouseReleased(MouseEvent event) 
		{
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
		 */
		public void mouseClicked(MouseEvent event) {
			// if mouse button was pressed twice times
			if (event.getClickCount() == 2) 
			{
				// get selected row
				int selectedRow = resourceTableController.getView().getSelectedRow();
				// get message node at selected row
				ResourceHeaderTableModel headerTableModel = (ResourceHeaderTableModel)resourceTableController.getHeaderTableModel();
				ResourceTreeNode node = headerTableModel.getMessageNodeAtRow(selectedRow);
				if(node!=null)
				{
					ResourceDialog resourceDialog = new ResourceDialog(getView().getFrame(), node.getResourceBean());
				    resourceDialog.setReadOnly();
					resourceDialog.setVisible(true);
				}
			}
		}

		protected void processPopup(final MouseEvent event) {

			isTablePopupEvent = true;

			JTable table = tableController.getView();

			int selectedRows = table.getSelectedRowCount();

			if (selectedRows <= 1&&selectedRows>0) {
				// select node
				int row = table.rowAtPoint(new Point(event.getX(), event.getY()));
				table.setRowSelectionInterval(row, row);
			}

			SwingUtilities.invokeLater(new Runnable() {

				public void run() {
					resourceTableController.getResourcePopupMenu().show(event.getComponent(),
							event.getX(), event.getY());
					isTablePopupEvent = false;
				}
			});
		}
	}

	class TreeMouseListener extends MouseAdapter {

		/**
		 * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
		 */
		public void mousePressed(MouseEvent event) {
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		public void mouseReleased(MouseEvent event) {
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
		 */
		public void mouseClicked(MouseEvent event) {
			// if mouse button was pressed twice times
			if (event.getClickCount() == 2) {
				// get selected row

			}
		}

		protected void processPopup(final MouseEvent event) {

			isTreePopupEvent = true;

			Point point = event.getPoint();
			TreePath path = treeController.getView().getClosestPathForLocation(
					point.x, point.y);
			treeController.getView().setSelectionPath(path);

			SwingUtilities.invokeLater(new Runnable() {

				public void run() {
					treeController.getPopupMenu().show(event.getComponent(),
							event.getX(), event.getY());
					isTreePopupEvent = false;
				}
			});
		}
	}
	
	class SocialNetMouseListener extends MouseAdapter {

		/**
		 * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
		 */
		public void mousePressed(MouseEvent event) {
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		public void mouseReleased(MouseEvent event) {
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
		 */
		public void mouseClicked(MouseEvent event) {
			// if mouse button was pressed twice times
			if (event.getClickCount() == 2) {
				// get selected row
			}
		}

		protected void processPopup(final MouseEvent event) {

			isTreePopupEvent = true;
			Point point = event.getPoint();
			SwingUtilities.invokeLater(new Runnable() {

				public void run() {
					snController.getPopupMenu().show(event.getComponent(),
							event.getX(), event.getY());
					isTreePopupEvent = false;
				}
			});
		}
	}
	
	class SearchResultMouseListener extends MouseAdapter {

		/**
		 * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
		 */
		public void mousePressed(MouseEvent event) {
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		public void mouseReleased(MouseEvent event) {
			if (event.isPopupTrigger()) {
				processPopup(event);
			}
		}

		/**
		 * @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
		 */
		public void mouseClicked(MouseEvent event) {
			// if mouse button was pressed twice times
			if (event.getClickCount() == 2) {
				// get selected row
			}
		}

		protected void processPopup(final MouseEvent event) {

			isTreePopupEvent = true;
			Point point = event.getPoint();
			SwingUtilities.invokeLater(new Runnable() {

				public void run() {
					resourceSearchTreeController.getPopupMenu().show(event.getComponent(),
							event.getX(), event.getY());
					isTreePopupEvent = false;
				}
			});
		}
	}

	/**
	 * @see org.columba.core.gui.frame.DefaultFrameController#close(org.columba.api.gui.frame.IContainer)
	 */
	public void close(IContainer container) {
		super.close(container);

		IMailFolderCommandReference r = getTreeSelection();

		if (r != null) {
			IMailFolder folder = (IMailFolder) r.getSourceFolder();

			// folder-based configuration

			if (folder instanceof IMailbox)
				getFolderOptionsController().save((IMailbox) folder);
		}
	}

	/**
	 * @see org.columba.core.gui.frame.DockFrameController#loadDefaultPosition()
	 */
	public void loadDefaultPosition() {

		super.dock(messageListDockable, IDock.REGION.CENTER);

		super.dock(folderTreeDockable, messageListDockable, IDock.REGION.WEST, 0.3f);
		//super.dock(messageViewerDockable, messageListDockable,IDock.REGION.SOUTH, 0.3f);
		super.dock(socialNetViewDockable, messageListDockable,IDock.REGION.SOUTH, 0.3f);
		super.setSplitProportion(folderTreeDockable, 0.3f);
		super.setSplitProportion(messageListDockable, 0.35f);
	}

	private void registerDockables() {

		JPopupMenu popup = null;

		// mail folder tree
		JScrollPane treeScrollPane = new JScrollPane(treeController.getView());
		treeScrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

		//result search tree
		JScrollPane resultTreeScrollPane = new JScrollPane(resourceSearchTreeController.getView());
		resultTreeScrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		folderTreeDockable = registerDockable("mail_foldertree",
				MailResourceLoader.getString("global", "dockable_foldertree"),
				treeScrollPane, new SortFoldersMenu(this));
		
		resourceListDockable = registerDockable("mail_resulttree",
				MailResourceLoader.getString("global", "dockable_resulttree"),
				resultTreeScrollPane, null);

		// message list
		JPanel p = new JPanel();
		p.setLayout(new BorderLayout());
		JScrollPane tableScrollPane = new JScrollPane(tableController.getView());
		tableScrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		p.add(tableScrollPane, BorderLayout.CENTER);
		p.add(filterToolbar, BorderLayout.NORTH);

		popup = null;
		try 
		{
			InputStream is = DiskIO
					.getResourceStream("org/columba/mail/action/table_dockmenu.xml");
			popup = new MenuXMLDecoder(this).createPopupMenu(is);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		messageListDockable = registerDockable("mail_messagelist",
				MailResourceLoader.getString("global", "dockable_messagelist"),
				p, popup);

		popup = null;
		try {
			InputStream is = DiskIO
					.getResourceStream("org/columba/mail/action/message_dockmenu.xml");
			popup = new MenuXMLDecoder(this).createPopupMenu(is);
		} catch (IOException e1) {
			e1.printStackTrace();
		}

		popup = null;
		messageViewerDockable = registerDockable("mail_messageviewer",
				MailResourceLoader
						.getString("global", "dockable_messageviewer"),
				messageController, popup);
		
		popup = null;
GraphZoomScrollPane scrollPane = new GraphZoomScrollPane(snController.getView());
		
		socialNetViewDockable = registerDockable("mail_hotcoreviewer", 
				MailResourceLoader.getString("global", "dockable_hotcoreviewer"),
				scrollPane, popup);
		
		// resouce list
		JPanel ps = new JPanel();
		ps.setLayout(new BorderLayout());
		JScrollPane resourceTableScrollPane = new JScrollPane(resourceTableController.getView());
		resourceTableScrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
		ps.add(resourceTableScrollPane, BorderLayout.CENTER);
		//ps.add(filterToolbar, BorderLayout.NORTH);
		
		resourceListDockable = registerDockable("mail_resourceviewer", 
				MailResourceLoader.getString("global", "dockable_resourceviewer"),
				ps, popup);

//		TagList tagList = new MailTagList(this);
//		JScrollPane tagListScrollPane = new JScrollPane(tagList);
//		tagListScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//		tagListScrollPane
//				.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

//		tagListDockable = registerDockable("mail_taglist", MailResourceLoader
//				.getString("global", "dockable_taglist"), tagListScrollPane,
//				new TagPopupMenu(this, tagList));
//		tagList.setPopupMenu(new TagPopupMenu(this, tagList));
	}

	/**
	 * ********************** container callbacks *************
	 */

	public void extendMenu(IContainer container) {
		try {
			InputStream is = DiskIO
					.getResourceStream("org/columba/mail/action/menu.xml");
			container.extendMenu(this, is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void extendToolBar(IContainer container) {
		try {
			File configDirectory = MailConfig.getInstance()
					.getConfigDirectory();
			InputStream is2 = new FileInputStream(new File(configDirectory,
					"main_toolbar.xml"));
			container.extendToolbar(this, is2);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @return Returns the messageViewerPanel.
	 */
	public IDockable getMessageViewerDockable() {
		return messageViewerDockable;
	}

	public void messageAdded(IFolderEvent e) {
	}

	public void messageRemoved(IFolderEvent e) {
	}

	public void messageFlagChanged(IFolderEvent e) {
	}

	public void folderPropertyChanged(IFolderEvent e) {
		// fire in EDT
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				updateTreeDockableTitle();
			}
		});
	}

	public void folderAdded(IFolderEvent e) {
	}

	public void folderRemoved(IFolderEvent e) {
	}

}